package com.team233.zuccecho.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.team233.zuccecho.Bean.Question;
import com.team233.zuccecho.Bean.Teacher;
import com.team233.zuccecho.Bean.Template;
import com.team233.zuccecho.dto.TemplateDto;
import com.team233.zuccecho.exception.EchoServiceException;
import com.team233.zuccecho.rabbitMQ.ZuccEchoMessage;
import com.team233.zuccecho.repository.QuestionRepository;
import com.team233.zuccecho.repository.TeacherRepository;
import com.team233.zuccecho.repository.TemplateRepository;
import com.team233.zuccecho.result.ExceptionMsg;
import com.team233.zuccecho.result.ResponseData;
import com.team233.zuccecho.service.inter.TemplateInterface;
import com.team233.zuccecho.util.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/v1/template")
public class TemplateController {
    private final Logger logger = LoggerFactory.getLogger(TemplateController.class);

    @Autowired
    private AmqpTemplate mqService;
    @Autowired
    private TemplateInterface templateInterface;
    @Qualifier("direct")
    @Autowired
    private DirectExchange directExchange;
    @Autowired
    private AmqpTemplate amqpTemplate;

    //根据教师id获取模板
    @RequestMapping(value = "/getall",method = RequestMethod.GET)
    public ResponseData getAll(String teacherid){
        List<String> briefInfo = templateInterface.getTemplatesBrief(teacherid);
        logger.warn("teacher get templates",teacherid);
        return new ResponseData(ExceptionMsg.SUCCESS,briefInfo.toString());
    }

    //获取特定模板
    @RequestMapping(value = "getone",method = RequestMethod.GET)
    public ResponseData getOne(long templateid){
        String template = templateInterface.getSpeTemplate(templateid);
        logger.warn("get template",templateid);
        return new ResponseData(ExceptionMsg.SUCCESS,template);
    }

    //添加模板
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public ResponseData createTemplate(@RequestBody TemplateDto templateDto){
        Long id = templateInterface.addTemplate(templateDto);
        logger.warn("teacher create template " + id);
//        if(id==null){
////            throw new EchoServiceException("新建模板不能上传id");
//            ZuccEchoMessage msg = new ZuccEchoMessage(ZuccEchoMessage.CATEGORY_ERROR);
//            msg.appendContent("op", "teacher_id "+templateDto.getTea_id()+" attemp to create a template with template_id");
//
//            mqService.convertAndSend(directExchange.getName(), Constants.KEY_WARN, msg);
//            return new ResponseData(ExceptionMsg.FAILED,null);
//        }
//        logger.warn("teacher create template "+id);

        //1.基础模式
        ZuccEchoMessage msg = new ZuccEchoMessage(ZuccEchoMessage.CATEGORY_MODEL_PUB);
        msg.appendContent("new template's id = : ", templateDto.getTemplate_id());
        msg.appendContent("new template's name = : ", templateDto.getTemplate_name());
        amqpTemplate.convertAndSend(Constants.QUE_SIMPLE);

        return new ResponseData(ExceptionMsg.SUCCESS,null);
    }
    //删除模板
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public ResponseData delteTemplate(@RequestBody TemplateDto templateDto){
        Integer done = templateInterface.deleteTemplate(templateDto);
        logger.warn("delete template");
        if(done == null)
            return new ResponseData(ExceptionMsg.FAILED,null);
        else
            return new ResponseData(ExceptionMsg.SUCCESS,done);
    }

    //修改模板
    @RequestMapping(value = "/modify",method = RequestMethod.POST)
    public ResponseData modifyTemplate(@RequestBody TemplateDto templateDto){
        Long id = templateInterface.modifyTemplate(templateDto);
        logger.warn("modify template",id);
        return new ResponseData(ExceptionMsg.SUCCESS,null);
    }

//    //tst
//    @RequestMapping(value = "/publish", method = RequestMethod.POST)
//    public ResponseData publishModel(){
//        logger.warn("执行模型的发布动作.....完成");
//
//        //模型发布成功，进行消息通知
//        ZuccEchoMessage msg = new ZuccEchoMessage(ZuccEchoMessage.CATEGORY_ERROR);
//        msg.appendContent("modelSid", 1);
//        msg.appendContent("modelName", "一个最新的模板名称");
//
//        mqService.convertAndSend(Constants.QUE_SIMPLE, msg);
//        return new ResponseData(ExceptionMsg.SUCCESS,null);
//    }
}
